package com.njupt.Tree;

/**
 * @Author: wujiaming
 * @CreateTime: 2024/12/10 10:42
 * @Description: 108. 将有序数组转换为二叉搜索树
 * @Version: 1.0
 */


public class SortedArrayToBST {

    public TreeNode sortedArrayToBST(int[] nums) {

        int left = 0;
        int right = nums.length-1;
        return toBST(nums,left,right);

    }

    private TreeNode toBST(int[] nums, int left, int right) {
        if(left > right){
            return null;
        }
//        int mid = (left + right) / 2;
        int mid = left + (right - left) / 2;
        TreeNode root = new TreeNode(nums[mid]);
        root.left = toBST(nums,left,mid-1);
        root.right = toBST(nums,mid+1,right);
        return root;

    }

    public static void main(String[] args) {
        int[] nums = {-10,-3,0,5,9};
        SortedArrayToBST test = new SortedArrayToBST();
        TreeNode treeNode = test.sortedArrayToBST(nums);
        System.out.println();

    }
}
